package com.cssy.xxkj.logic.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.byd.acegiext.domain.User;
import com.cssy.xxkj.logic.dao.LogicWarehousePriceDAO;
import com.cssy.xxkj.logic.entity.LogicWarehousePriceDO;
import com.cssy.xxkj.logic.service.ILogicWarehousePriceService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;

@Service("logicWarehousePriceService")
public class LogicWarehousePriceServiceImpl implements ILogicWarehousePriceService{
	@Resource(name = "logicWarehousePriceDAO")
	private LogicWarehousePriceDAO logicWarehousePriceDAO;
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWarehousePriceDO>  list=logicWarehousePriceDAO.find(scs);
		rs.setRecordCount(logicWarehousePriceDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		Integer i =logicWarehousePriceDAO.deleteLogicWarehousePriceDOByPrimaryKey(id);
		return null;
	}

	@Override
	public Integer update(LogicWarehousePriceDO t, User user) throws SyBaseException {
		t.setBizModifyName(user.getName());
		t.setBizModifyDate(new Date());
		return logicWarehousePriceDAO.updateLogicWarehousePriceDO(t);
	}

	@Override
	public LogicWarehousePriceDO save(LogicWarehousePriceDO t, User user) throws SyBaseException {
		t.setBizCreatorName(user.getName());
		t.setBizCreatorDate(new Date());
		logicWarehousePriceDAO.insertLogicWarehousePriceDO(t);
		return t;
	}

	@Override
	public LogicWarehousePriceDO findListById(String id) throws SyBaseException {
		return logicWarehousePriceDAO.findLogicWarehousePriceDOByPrimaryKey(id);
	}
	
	/**
	 * 输入物料运费计算新的平均单价
	 * @param t 包含有物料信息及所储位置信息的对象
	 * @param freight 运费
	 * @param user 当前登录用户信息
	 * @return
	 * @throws SyBaseException
	 */
	@Override
	public Integer updateAveragePrice(LogicWarehousePriceDO t, double freight, User user) throws SyBaseException{
		return null;//this.update(lpd, user);
	}
	
	@Override
	public Map<String, BigDecimal> findQueryMaterOutFoldToMap(SearchConditions foldScs)throws SyBaseException{
		List<LogicWarehousePriceDO> lwpriceList = logicWarehousePriceDAO.find(foldScs);
		if(lwpriceList==null||lwpriceList.isEmpty()||lwpriceList.size()==0){
			return null;
		}else{
			Map<String, BigDecimal> foldMap = new HashMap<String, BigDecimal>();
			for(LogicWarehousePriceDO lwprice : lwpriceList){
				if(lwprice==null||StringUtils.isBlank(lwprice.getId())){
					continue;
				}
				String keyStr = (StringUtils.isEmpty(lwprice.getZd001MaterialsId())?null:lwprice.getZd001MaterialsId())+"_"+
								(StringUtils.isEmpty(lwprice.getZd001MaterialsCode())?null:lwprice.getZd001MaterialsCode())+"_"+
								(StringUtils.isEmpty(lwprice.getZd001MaterialsName())?null:lwprice.getZd001MaterialsName())+"_"+
								(StringUtils.isEmpty(lwprice.getZd001MaterialsModel())?null:lwprice.getZd001MaterialsModel())+"_"+
								(StringUtils.isEmpty(lwprice.getZd001MaterialsSpec())?null:lwprice.getZd001MaterialsSpec())+"_"+
								(StringUtils.isEmpty(lwprice.getZd001MaterialsColor())?null:lwprice.getZd001MaterialsColor())+"_"+
								(StringUtils.isEmpty(lwprice.getZd001MaterialsQuanlity())?null:lwprice.getZd001MaterialsQuanlity());
//				BigDecimal foldMoney = BigDecimal.ZERO;
				if(lwprice.getZd006SumQuantity().compareTo(BigDecimal.ZERO)==0||lwprice.getZd007SumPrice().compareTo(BigDecimal.ZERO)==0){
					continue;
				}
				System.out.println(lwprice.getId());
				foldMap.put(keyStr, (lwprice.getZd007SumPrice()==null?BigDecimal.ZERO:lwprice.getZd007SumPrice()).divide(lwprice.getZd006SumQuantity(),4));
			}
			return foldMap;
		}
		
		
		
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

}
